package ru.simplemaps.osmapi;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * Simple OMS service. just loads the data
 *
 * @author enaku_adm
 * @since 22.07.2010 17:00:26
 */
public class OSMService {
    private final String url;
    private final HttpClient client;

    public static final String DEFAULT_URL = "http://api.openstreetmap.org";

    public OSMService(HttpClient client) {
        this.url = DEFAULT_URL;
        this.client = client;
    }

    public OSMService(HttpClient client, String url) {
        this.url = url;
        this.client = client;
    }

    private String makeUrl(double left, double bottom, double right, double top) {
        String s = url + "/api/0.6/map?bbox=" + left + "," + bottom + "," + right + "," + top;
        System.out.println(s);
        return s;
    }

    public OSMData loadData(double left, double bottom, double right, double top) throws IOException, SAXException, ParserConfigurationException {
        HttpResponse response = client.execute(new HttpGet(makeUrl(left, bottom, right, top)));
        HttpEntity entity = response.getEntity();
        return OSMParser.parse(entity.getContent());
    }
}
